package com.licheng.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.licheng.mapper.EmpExprMapper;
import com.licheng.mapper.EmpMapper;
import com.licheng.pojo.*;
import com.licheng.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    /**
     * 员工分页查询显示
     * @param epq
     * @return
     */
    @Override
    public EmpPageResult selectEmpPage(EmpPageQuery epq) {
        PageHelper.startPage(epq.getPage(), epq.getPageSize());

        List<Emp> empList = empMapper.selectEmpPage(epq);
        Page p = (Page) empList;

        Long total = p.getTotal();
        List<Emp> rows = p.getResult();
        return new EmpPageResult(total, rows);
    }

    /**
     * 删除员工
     * @param ids 员工id
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void deleteEmp(List<Integer> ids) {
        //删除员工基本信息
        empMapper.deleteEmp(ids);
        //删除员工工作经历
        empExprMapper.deleteExpr(ids);
    }

    /**
     * 插入员工信息
     * @param emp
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void add(Emp emp) {
        //1、插入员工emp
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);

        //2、插入员工经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(emp.getId());
            }
            empExprMapper.addExpr(exprList);
        }

    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public Emp selectById(Integer id) {
        Emp emp = empMapper.selectById(id);
        return emp;
    }

    /**
     * 修改员工
     * @param emp
     */
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        //1、修改员工信息
        empMapper.updateEmp(emp);

        //2、删除员工工作经历
        List<Integer> id = new ArrayList<>();
        id.add(emp.getId());
        empExprMapper.deleteExpr(id);

        //3、插入员工经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(emp.getId());
            }
            empExprMapper.addExpr(exprList);
        }
    }

    /**
     * 查询所有员工
     * @return
     */
    @Override
    public List<Emp> selectAll() {
        List<Emp> empList = empMapper.selectAll();
        return empList;
    }

    /**
     * 修改密码
     * @param changePass
     */
    @Override
    public Result changePass(ChangePass changePass) {
        Integer row = empMapper.changePass(changePass);
        log.info("修改{}", row);
        if(row == 1){
            return Result.success();
        }return Result.error("旧密码不正确");
    }

}
